
#include "stdafx.h"

#include "PropertiesWnd.h"
#include "Resource.h"
#include "MainFrm.h"
#include "AnalyticDrawing.h"
#include "AnalyticDrawingView.h"
#include "display/OpenGLDisplay.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

/////////////////////////////////////////////////////////////////////////////
// CResourceViewBar

CPropertiesWnd::CPropertiesWnd()
{
}

CPropertiesWnd::~CPropertiesWnd()
{
}

BEGIN_MESSAGE_MAP(CPropertiesWnd, CDockablePane)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_COMMAND(ID_EXPAND_ALL, OnExpandAllProperties)
	ON_UPDATE_COMMAND_UI(ID_EXPAND_ALL, OnUpdateExpandAllProperties)
	ON_COMMAND(ID_SORTPROPERTIES, OnSortProperties)
	ON_UPDATE_COMMAND_UI(ID_SORTPROPERTIES, OnUpdateSortProperties)
// 	ON_COMMAND(ID_PROPERTIES1, OnProperties1)
// 	ON_UPDATE_COMMAND_UI(ID_PROPERTIES1, OnUpdateProperties1)
// 	ON_COMMAND(ID_PROPERTIES2, OnProperties2)
// 	ON_UPDATE_COMMAND_UI(ID_PROPERTIES2, OnUpdateProperties2)
	ON_WM_SETFOCUS()
	ON_WM_SETTINGCHANGE()
	ON_REGISTERED_MESSAGE(AFX_WM_PROPERTY_CHANGED, OnPropertyChanged)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResourceViewBar message handlers

void CPropertiesWnd::AdjustLayout()
{
	if (GetSafeHwnd() == NULL)
	{
		return;
	}

	CRect rectClient;
	GetClientRect(rectClient);

	int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;
	m_wndToolBar.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
	m_wndPropList.SetWindowPos(NULL, rectClient.left, rectClient.top + cyTlb, rectClient.Width(), rectClient.Height() - cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
// 	CRect rectClient,rectCombo;
// 	GetClientRect(rectClient);

//	m_wndObjectCombo.GetWindowRect(&rectCombo);

// 	int cyCmb = rectCombo.Size().cy;
// 	int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;

//	m_wndObjectCombo.SetWindowPos(NULL, rectClient.left, rectClient.top, rectClient.Width(), 200, SWP_NOACTIVATE | SWP_NOZORDER);
// 	m_wndToolBar.SetWindowPos(NULL, rectClient.left, rectClient.top + cyCmb, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
//	m_wndPropList.SetWindowPos(NULL, rectClient.left, rectClient.top + cyCmb + cyTlb, rectClient.Width(), rectClient.Height() -(cyCmb+cyTlb), SWP_NOACTIVATE | SWP_NOZORDER);
}

int CPropertiesWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	CRect rectDummy;
	rectDummy.SetRectEmpty();

	// Create combo:
	const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST | WS_BORDER | CBS_SORT | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

// 	if (!m_wndObjectCombo.Create(dwViewStyle, rectDummy, this, 1))
// 	{
// 		TRACE0("Failed to create Properties Combo \n");
// 		return -1;      // fail to create
// 	}
// 
// 	m_wndObjectCombo.AddString(_T("Application"));
// 	m_wndObjectCombo.AddString(_T("Properties Window"));
// 	m_wndObjectCombo.SetCurSel(0);

	if (!m_wndPropList.Create(WS_VISIBLE | WS_CHILD, rectDummy, this, 2))
	{
		TRACE0("Failed to create Properties Grid \n");
		return -1;      // fail to create
	}

	InitPropList();

	m_wndToolBar.Create(this, AFX_DEFAULT_TOOLBAR_STYLE, IDR_PROPERTIES);
	m_wndToolBar.LoadToolBar(IDR_PROPERTIES, 0, 0, TRUE /* Is locked */);
	m_wndToolBar.CleanUpLockedImages();
	m_wndToolBar.LoadBitmap(theApp.m_bHiColorIcons ? IDB_PROPERTIES_HC : IDR_PROPERTIES, 0, 0, TRUE /* Locked */);

	m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
	m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
	m_wndToolBar.SetOwner(this);

	// All commands will be routed via this control , not via the parent frame:
	m_wndToolBar.SetRouteCommandsViaFrame(FALSE);

	AdjustLayout();
	return 0;
}

void CPropertiesWnd::OnSize(UINT nType, int cx, int cy)
{
	CDockablePane::OnSize(nType, cx, cy);
	AdjustLayout();
}

void CPropertiesWnd::OnExpandAllProperties()
{
	m_wndPropList.ExpandAll();
}

void CPropertiesWnd::OnUpdateExpandAllProperties(CCmdUI* /* pCmdUI */)
{
}

void CPropertiesWnd::OnSortProperties()
{
	m_wndPropList.SetAlphabeticMode(!m_wndPropList.IsAlphabeticMode());
}

void CPropertiesWnd::OnUpdateSortProperties(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(m_wndPropList.IsAlphabeticMode());
}

// void CPropertiesWnd::OnProperties1()
// {
// 	// TODO: Add your command handler code here
// }
// 
// void CPropertiesWnd::OnUpdateProperties1(CCmdUI* /*pCmdUI*/)
// {
// 	// TODO: Add your command update UI handler code here
// }
// 
// void CPropertiesWnd::OnProperties2()
// {
// 	// TODO: Add your command handler code here
// }
// 
// void CPropertiesWnd::OnUpdateProperties2(CCmdUI* /*pCmdUI*/)
// {
// 	// TODO: Add your command update UI handler code here
// }

void CPropertiesWnd::InitPropList()
{
	SetPropListFont();

	m_wndPropList.EnableHeaderCtrl(FALSE);
	m_wndPropList.EnableDescriptionArea();
	m_wndPropList.SetVSDotNetLook();
	m_wndPropList.MarkModifiedProperties();

	CString caption;

	CMFCPropertyGridProperty* pLight = new CMFCPropertyGridProperty(_T("Light"));

		CMFCPropertyGridProperty* pLightPosition = new CMFCPropertyGridProperty(_T("Light Position"));
			caption.LoadStringW(IDS_STRING_LIGHT_X_POS);
			CMFCPropertyGridProperty* pProp = new CMFCPropertyGridProperty(caption, (_variant_t)40, _T("Indicate the x index of the light source"));
			pLightPosition->AddSubItem(pProp);
			caption.LoadStringW(IDS_STRING_LIGHT_Y_POS);
			pProp = new CMFCPropertyGridProperty(caption, (_variant_t)50, _T("Indicate the y index of the light source"));
			pLightPosition->AddSubItem(pProp);
			caption.LoadStringW(IDS_STRING_LIGHT_Z_POS);
			pProp = new CMFCPropertyGridProperty(caption, (_variant_t)30, _T("Indicate the z index of the light source"));
			pLightPosition->AddSubItem(pProp);

		pLight->AddSubItem(pLightPosition);

		caption.LoadStringW(IDS_STRING_LIGHT_DIFF);
		CMFCPropertyGridColorProperty* pLightColor = new CMFCPropertyGridColorProperty(caption, RGB(0, 0, 255), NULL,
			_T("Specify the light diffusion density"));
		pLightColor->EnableOtherButton(_T("Other..."));
		//pLightColor->EnableAutomaticButton(_T("Default"), RGB(0, 0, 255));
		pLight->AddSubItem(pLightColor);

		caption.LoadStringW(IDS_STRING_LIGHT_SPEC);
		pLightColor = new CMFCPropertyGridColorProperty(caption, RGB(0, 0, 255), NULL,
			_T("Specify the light specular density"));
		pLightColor->EnableOtherButton(_T("Other..."));
		//pLightColor->EnableAutomaticButton(_T("Default"), RGB(0, 0, 255));
		pLight->AddSubItem(pLightColor);

		caption.LoadStringW(IDS_STRING_LIGHT_AMB);
		pLightColor = new CMFCPropertyGridColorProperty(caption, RGB(26, 26, 78), NULL,
			_T("Specify the light ambient density"));
		pLightColor->EnableOtherButton(_T("Other..."));
		//pLightColor->EnableAutomaticButton(_T("Default"), RGB(26, 26, 78));
		pLight->AddSubItem(pLightColor);

	m_wndPropList.AddProperty(pLight);

	CMFCPropertyGridProperty* pMaterial = new CMFCPropertyGridProperty(_T("Material"));
		caption.LoadStringW(IDS_STRING_MAT_SHINE);
		pProp = new CMFCPropertyGridProperty(caption, (_variant_t)90, _T("Indicate the material shiness"));
		pMaterial->AddSubItem(pProp);
		caption.LoadStringW(IDS_STRING_MAT_AMB);
		CMFCPropertyGridColorProperty* pMaterialColor = new CMFCPropertyGridColorProperty(caption, RGB(255, 255, 255), NULL,
			_T("Specify the material ambient density"));
		pMaterialColor->EnableOtherButton(_T("Other..."));
		//pMaterialColor->EnableAutomaticButton(_T("Default"), RGB(255, 255, 255));
		pMaterial->AddSubItem(pMaterialColor);
		caption.LoadStringW(IDS_STRING_MAT_SPEC);
		pMaterialColor = new CMFCPropertyGridColorProperty(caption, RGB(255, 255, 255), NULL,
			_T("Specify the material specular density"));
		pMaterialColor->EnableOtherButton(_T("Other..."));
		//pMaterialColor->EnableAutomaticButton(_T("Default"), RGB(255, 255, 255));
		pMaterial->AddSubItem(pMaterialColor);

	m_wndPropList.AddProperty(pMaterial);
	
	CMFCPropertyGridProperty* pGroupSketch = new CMFCPropertyGridProperty(_T("Sketch"));
		caption.LoadStringW(IDS_STRING_SKE_CURVE);
		pProp = new CMFCPropertyGridProperty(caption, (_variant_t)8, _T("Specify curve segments"));
		pGroupSketch->AddSubItem(pProp);
		caption.LoadStringW(IDS_STRING_SKE_LINE);
		pProp = new CMFCPropertyGridProperty(caption, (_variant_t)1.2f, _T("Specify the line width in sketch"));
		pGroupSketch->AddSubItem(pProp);
		caption.LoadStringW(IDS_STRING_SKE_POINT);
		pProp = new CMFCPropertyGridProperty(caption, (_variant_t)6.0f, _T("Specify the point size in sketch"));
		pGroupSketch->AddSubItem(pProp);
		caption.LoadStringW(IDS_STRING_SKE_LINE_COLOR);
		CMFCPropertyGridColorProperty* pSkeColor = new CMFCPropertyGridColorProperty(caption, RGB(25, 25, 25), NULL,
			_T("Specify the line color"));
		pSkeColor->EnableOtherButton(_T("Other..."));
		pGroupSketch->AddSubItem(pSkeColor);
		caption.LoadStringW(IDS_STRING_SKE_SCA_COLOR);
		pSkeColor = new CMFCPropertyGridColorProperty(caption, RGB(25, 225, 25), NULL, _T("Specify the scaffold color"));
		pSkeColor->EnableOtherButton(_T("Other..."));
		pGroupSketch->AddSubItem(pSkeColor);
		caption.LoadString(IDS_STRING_SKE_POINT_COLOR);
		pSkeColor = new CMFCPropertyGridColorProperty(caption, RGB(25, 225, 225), NULL, _T("Specify the point color"));
		pSkeColor->EnableOtherButton(_T("Other..."));
		pGroupSketch->AddSubItem(pSkeColor);
		caption.LoadStringW(IDS_STRING_SKE_ACTIVE_VERTEX);
		pSkeColor = new CMFCPropertyGridColorProperty(caption, RGB(225, 225, 225), NULL, _T("Specify the active vertex color"));
		pSkeColor->EnableOtherButton(_T("Other"));
		pGroupSketch->AddSubItem(pSkeColor);
	m_wndPropList.AddProperty(pGroupSketch);
}

void CPropertiesWnd::OnSetFocus(CWnd* pOldWnd)
{
	CDockablePane::OnSetFocus(pOldWnd);
	m_wndPropList.SetFocus();
}

void CPropertiesWnd::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
	CDockablePane::OnSettingChange(uFlags, lpszSection);
	SetPropListFont();
}

void CPropertiesWnd::SetPropListFont()
{
	::DeleteObject(m_fntPropList.Detach());

	LOGFONT lf;
	afxGlobalData.fontRegular.GetLogFont(&lf);

	NONCLIENTMETRICS info;
	info.cbSize = sizeof(info);

	afxGlobalData.GetNonClientMetrics(info);

	lf.lfHeight = info.lfMenuFont.lfHeight;
	lf.lfWeight = info.lfMenuFont.lfWeight;
	lf.lfItalic = info.lfMenuFont.lfItalic;

	m_fntPropList.CreateFontIndirect(&lf);

	m_wndPropList.SetFont(&m_fntPropList);
//	m_wndObjectCombo.SetFont(&m_fntPropList);
}

LRESULT CPropertiesWnd::OnPropertyChanged(WPARAM, LPARAM lParam)
{
	CMFCPropertyGridProperty* pProp = (CMFCPropertyGridProperty*)lParam;
	CString s = pProp->GetName();
	COleVariant var = pProp->GetValue();
	CMainFrame* pFrame = (CMainFrame *)AfxGetMainWnd();
	CAnalyticDrawingView* pView = (CAnalyticDrawingView *)(pFrame->GetActiveView());
	COpenGLDisplay* pDisplay = pView->GetOpenGLDisplay();
	CString caption;

	if (caption.LoadStringW(IDS_STRING_LIGHT_X_POS) && !s.Compare(caption))
	{
		pDisplay->GetLight(0)->SetLightXPos(var.lVal);
	}
	else if (caption.LoadStringW(IDS_STRING_LIGHT_Y_POS) && !s.Compare(caption))
	{
		pDisplay->GetLight(0)->SetLightYPos(var.lVal);
	}
	else if (caption.LoadStringW(IDS_STRING_LIGHT_Z_POS) && !s.Compare(caption))
	{
		pDisplay->GetLight(0)->SetLightZPos(var.lVal);
	}
	else if (caption.LoadStringW(IDS_STRING_MAT_SHINE) && !s.Compare(caption))
	{
		pDisplay->GetMaterial()->SetMaterialParas(NULL, NULL, var.lVal);
	}
	else if (caption.LoadStringW(IDS_STRING_SKE_CURVE) && !s.Compare(caption))
	{
		pDisplay->GetSketchPaint()->SetCurveSegment(var.lVal);
		CAnalyticDrawingDoc* pDoc = pView->GetDocument();
		CSketchModel* pModel = pDoc->GetSketchModel();
		pModel->InferSurface(pDisplay->GetSketchPaint()->GetCurveSegment());
		pFrame->UpdateStatusBar(pModel->GetVertexCountWithoutInfPoint(), pModel->GetLineSegmentCount(), 
			pModel->GetScaffoldCount(), pModel->GetCurveCount(), pModel->GetSurfaceCount());
	}
	else if (caption.LoadStringW(IDS_STRING_SKE_LINE) && !s.Compare(caption))
	{
		pDisplay->GetSketchPaint()->SetLineWidth(var.fltVal);
	}
	else if (caption.LoadStringW(IDS_STRING_SKE_POINT) && !s.Compare(caption))
	{
		pDisplay->GetSketchPaint()->SetPointSize(var.fltVal);
	}

	CMFCPropertyGridColorProperty* pColorProp = (CMFCPropertyGridColorProperty *)lParam;
	COLORREF color = pColorProp->GetColor();
	float r, g, b;
	r = GetRValue(color) * 1.0 / 255;
	g = GetGValue(color) * 1.0 / 255;
	b = GetBValue(color) * 1.0 / 255;
	float colors[] = {r, g, b, 1.0f};
	if (caption.LoadStringW(IDS_STRING_LIGHT_DIFF) && !s.Compare(caption))
	{
		pDisplay->GetLight(0)->SetLightParas(NULL, colors, NULL);
	}
	else if (caption.LoadStringW(IDS_STRING_LIGHT_SPEC) && !s.Compare(caption))
	{
		pDisplay->GetLight(0)->SetLightParas(NULL, NULL, colors);
	}
	else if (caption.LoadStringW(IDS_STRING_LIGHT_AMB) && !s.Compare(caption))
	{
		pDisplay->GetLightManeger()->SetAmbientLight(colors);
	}
	else if (caption.LoadStringW(IDS_STRING_MAT_AMB) && !s.Compare(caption))
	{
		pDisplay->GetMaterial()->SetMaterialParas(colors, NULL, -1);
	}
	else if (caption.LoadStringW(IDS_STRING_MAT_SPEC) && !s.Compare(caption))
	{
		pDisplay->GetMaterial()->SetMaterialParas(NULL, colors, -1);
	}
	else if (caption.LoadStringW(IDS_STRING_SKE_LINE_COLOR) && !s.Compare(caption))
	{
		pDisplay->GetSketchPaint()->SetLineColor(colors);
	}
	else if (caption.LoadStringW(IDS_STRING_SKE_SCA_COLOR) && !s.Compare(caption))
	{
		pDisplay->GetSketchPaint()->SetScaffoldColor(colors);
	}
	else if (caption.LoadStringW(IDS_STRING_SKE_POINT_COLOR) && !s.Compare(caption))
	{
		pDisplay->GetSketchPaint()->SetPointColor(colors);
	}
	else if (caption.LoadStringW(IDS_STRING_SKE_ACTIVE_VERTEX) && !s.Compare(caption))
	{
		pDisplay->GetSketchPaint()->SetActiveVertexColor(colors);
	}
	pView->InvalidateRect(NULL, FALSE);
	return 0;
}
